<?php

/**
 * Teacher页
 */

namespace backend\controllers;

use bc\controller\BackendController;
use bc\util\backend\Page;
use bc\model\knowledge\TeacherModel;
use bc\util\Uploader;

class TeacherController extends BackendController
{
    
    public function  actionIndex()
    {
        $model = new TeacherModel();    
        $params = null;
		
                $model->autoId='teacher_id';
		$page = $this->get('page', 1);
		//$model->sort = 't_sort DESC';
                $params = '1 = 1';
		
		$teacher_name = $this->get('teacher_name');
		
		if($teacher_name)
		{
			$params .= ' AND teacher_name LIKE "%' . $teacher_name . '%"'; 
		}
		
		
		$page = $this->get('page', 1);
		$model->sort = 'teacher_id DESC';

		$list = $model->getList($params, $page, self::PAGE_SIZE);
		
		$total = $model->getTotal($params);
		
		$pagination = new Page($total, self::PAGE_SIZE);
		
		$view['list'] = $list;
		$view['pagination'] = $pagination;
                $view['teacher_name'] = $teacher_name;
		
		return $this->render('index', $view);    
        
    }
    public function actionAdd()
	{
		return $this->render('add');
	}
    
    public function actionSave()
    {           
               
                if($this->getRequest()->getMethod() === 'POST')
                {
                        $model = new TeacherModel();
                        $id = $this->post('id', 0);
                        
                        $data['teacher_name'] = $this->post('teacher_name', '');
			$data['title'] = $this->post('title', '');
                       
                        //图片上传
                        if($_FILES['teacher_image'])
			{       
				$up = new Uploader( 'teacher_image' , $this->getUploadConfig() );
				$info = $up->getFileInfo();
				if($info['state'] === 'SUCCESS')
				{
					$data['teacher_image'] = str_replace(UPLOAD_PATH, '', $info['url']);
				}
					
			}
                        if($id)
			{       $data['update_time'] =  date("Y-m-d H:i:s");
				$model->modify($data, $id);
			}
			else
			{       
                                $data['teacher_status'] = 1;
				$data['create_time'] = date("Y-m-d H:i:s");
				$model->add($data);
			}
			$output['msg'] = '操作成功!';
                        $output['url'] = '/teacher/index';
                        $this->forward($output);
                }
                        else
                        {
			$output['msg'] = '非法请求!';
                        }
		
                                                             
                }             
    
    public function actionEdit()
    {   
                       
                $id = $this->get('id');
		if(!isset($id))
			$this->redirect('/');
	
		$model = new TeacherModel();
                $model->autoId='teacher_id';
		$row = $model->getRowById($id);
		
		if(!$row)
			$this->redirect('/');
	
		$view['row'] = $row;
	
		return $this->render('edit', $view);
    }
    
    public function  actionDelete()
    {           
                $id = $this->get('id');
                $model = new TeacherModel();
                $data = $model ->getRowById($id);
               // var_dump($data);die();
                if($id)
			{       
                                if($data['teacher_status'] == 1){
                                    $data['teacher_status'] = 0;
                                }elseif($data['teacher_status'] == 0){
                                    $data['teacher_status'] = 1;
                                }
                                    
				$model->modify($data, $id);
                                
                                $output['msg'] = '操作成功!';
                                $output['url'] = '/teacher/index';
                                $this->forward($output);
			}
			else
			{       
                            $this->redirect('/');                              				
			}
        
    }
    
    private function getUploadConfig()
	{
		//上传文件目录
		$path = UPLOAD_PATH . 'upload/';
		//上传配置
		$config = array(
				"savePath" => $path ,             //存储文件夹
				"maxSize" => 100000 ,                   //允许的文件最大尺寸，单位KB
				"allowFiles" => array( ".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp" )  //允许的文件格式
		);
			
		//背景保存在临时目录中
		$config[ "savePath" ] = $path;
		return $config;
	}
}

